﻿// Inner Fire 游戏引擎库
// DirectX窗口类
//
// Copyright (c) 2023 by 尔西大帝. All rights reserved.
//
// 这个类专用于DirectX的Window类。
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2024-11-15

#pragma once

#define DX_WINDOW_CLASS_NAME "DxWindowClass"
#define DX_WINDOW_NAME "DxEngine"

#include "dx_graphics.h"
#include <Windows.h>
#include <Windowsx.h>
#include <dx_core/camera.h>
#include <dx_scene/dx_scene.h>
#include <ikit.h>

namespace ifire {

class DxWindow {
public:
  DxWindow();
  ~DxWindow();

  bool OpenWindow(HWND parent, RECT rect);
  void CheckCompileShader();

  // 游戏主循环
  bool Run();

  WPARAM GetMsgWParam() const { return msg_.wParam; }
  UINT GetWidth() const { return width_; }
  UINT GetHeight() const { return height_; }
  HWND GetHandle() const { return hwnd_; }
  bool IsExited() const { return app_exited_; }
  bool IsCreated() const { return created_; }
  void SetTitle(const char* title) const { SetWindowText(hwnd_, title); }
  void Reset();
  void Close();
  void SetScene(DxScene* scene) { scene_ = scene; }

  static DxWindow* GetApp() { return dx_app_instance_; }
  LRESULT CALLBACK WndProc(
      HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  // 此代码模块中包含的函数的前向声明:
  ATOM RegisterWinClass();

  void OnMouseDown(WPARAM btn_state, int x, int y);
  void OnMouseUp(WPARAM btn_state, int x, int y);
  void OnMouseMove(WPARAM btn_state, int x, int y);

private:
  void OnResize(WPARAM wParam, LPARAM lParam);
  void Update();

private:
  std::unique_ptr<DxGraphics> dx_;
  bool created_;
  HWND hwnd_;
  HWND parent_;
  MSG msg_;
  UINT width_, height_;
  UINT client_width_, client_height_;
  bool app_paused_;
  bool app_exited_; // 是否已经退出
  bool is_resizing_;
  bool minimized_;
  bool maximized_;
  static DxWindow* dx_app_instance_;
  // 用于太阳光
  float sum_theta_ = 1.25f * XM_PI;
  float sum_phi_ = XM_PIDIV4;
  DxScene* scene_;
};

} // namespace ifire
